package com.whately.core.base.jwt;

import com.whately.core.base.constant.LoginUser;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@ToString
public class JwtLoginUserUtil {
    private LoginUser loginUser;
    private  String userId;

    private String userName;

    public JwtLoginUserUtil(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        loginUser = FetchLoginUserHelper.getLoginUserWithDebugBuild(request);
        if(loginUser!=null){
            userId = loginUser.getUserId();
            userName = loginUser.getUserName();
        }

    }

    public LoginUser getLoginUser(){
        return loginUser;
    }
    public String getUserId(){
        return userId;

    }

    public Long getUserIdNumber(){
        return StringUtils.isBlank(userId)?0:Long.parseLong(userId);

    }

    public String getUserName(){
        return userName;
    }
}
